package dianmobile.eistarandroid.ui.fragments;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import dianmobile.eistarandroid.R;
import dianmobile.eistarandroid.ui.activities.BoardListActivity;
import dianmobile.eistarandroid.utils.ConstantsData;


public class AllGroupFragment extends Fragment implements AdapterView.OnItemClickListener{

    private final String TAG = getClass().getSimpleName();

    private GridView mGridView;
    private String IMAGE_ITEM = "group_image";
    private String TEXT_ITEM = "group_name";

    /*test*/
    public AllGroupFragment() {
        Log.d(TAG, "new allGroupFragment");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceSt) {
        Log.d(TAG, "onCreateView ");
        return inflater.inflate(R.layout.fragment_all_group, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        Log.d(TAG, "onActivityCreate");

        super.onActivityCreated(savedInstanceState);
        mGridView = (GridView)getActivity().findViewById(R.id.gv_all_group);
        SimpleAdapter adapter = new SimpleAdapter( getActivity(),
                getGridViewData(),
                R.layout.group_item,
                new String[] {IMAGE_ITEM, TEXT_ITEM},
                new int[] {R.id.iv_group_image, R.id.tv_group_name});
        mGridView.setAdapter(adapter);
        mGridView.setOnItemClickListener(this);
    }

    /*test*/
    @Override
    public void onAttach(Activity activity) {
        Log.d(TAG, "onAttach");
        super.onAttach(activity);
    }
    @Override
    public void onCreate(Bundle savedInstanceState){
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);
    }
    @Override
    public void onDestroyView() {
        Log.d(TAG, "onDestroyView");
        super.onDestroy();
    }
    @Override
    public void onDestroy(){
        Log.d(TAG, "onDestroy");
        super.onDestroy();
    }

    /**
     *get the gridView data
     * @return data to show at gridView
     */
    private List<HashMap<String, Object>> getGridViewData() {

        //set the group name and image
         String[] arrGroupName = new String[] {
                 ConstantsData.GROUP_EISTAR, ConstantsData.GROUP_EI, ConstantsData.GROUP_DIGITAL,
                 ConstantsData.GROUP_SCIENCE, ConstantsData.GROUP_ART, ConstantsData.GROUP_SINCERE,
                 ConstantsData.GROUP_ENTERTAINMENT, ConstantsData.GROUP_NEWS
        };

        int[] arrGroupImage = new int[]{
                R.drawable.ic_bbs_system, R.drawable.ic_area_and_universities, R.drawable.ic_computer,
                R.drawable.ic_science, R.drawable.ic_literature, R.drawable.ic_campus_public,
                R.drawable.ic_entertainment, R.drawable.ic_sports
        };

        List<HashMap<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < arrGroupName.length; i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put(IMAGE_ITEM, arrGroupImage[i]);
            map.put(TEXT_ITEM, arrGroupName[i]);
            list.add(map);
        }
        return list;
    }

    /**
     * deal with click
     * @param parent the adapterView
     * @param view  the view clicked
     * @param position the position of clicked view
     * @param id the id of clicked view
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        HashMap<String, Object> item = (HashMap<String, Object>) parent.getItemAtPosition(position);

        Intent intent = new Intent(getActivity(), BoardListActivity.class);
        intent.putExtra("groupName", (String)item.get(TEXT_ITEM));
        startActivity(intent);
    }
}
